********************************************************************************
*Table A4: Heterogeneity by household income level				  		   	   *
*Code to run this file: 													   *
*texdoc do "${workdir}\Do files\Table A4 - het income.do"					   *
********************************************************************************

reg vacunada_int i.tratamiento##i.ingreso
local n1 = subinstr(string(e(N),"%12.0fc")," ","",.) 
local r21 = string(e(r2), "%9.3f")  
eststo model1_i

reg vacunada_int i.tratamiento##i.ingreso i.ips dosis_prev_int edad17
local n2 = subinstr(string(e(N),"%12.0fc")," ","",.) 
local r22 = string(e(r2), "%9.3f")  
eststo model2_i


local tratamiento_labels "Control Placebo Information `"Social norms"' Trust Framing"

// Generate the table with the estimated effects  
texdoc init "${tables}/TA4 het income (m).tex", replace
texdoc write \begin{table}[h!]
texdoc write \centering
texdoc write \caption{\centering Effect on HPV vaccine uptake by household income level} 
texdoc write \small
texdoc write \label{hetinc}
texdoc write \resizebox{0.9\textwidth}{!}{
texdoc write \begin{tabular}{p{0.5\linewidth}>{\hsize=0.8\hsize}p{0.25\linewidth}>{\hsize=0.8\hsize}p{0.25\linewidth}}
texdoc write \hline
texdoc write  & \multicolumn{1}{c}{Model 1} & \multicolumn{1}{c}{Model 2} \\
texdoc write \hline
texdoc write \addlinespace

texdoc write \textbf{Treatment} & & \\ 

// Write the estimates for i.tratamiento  
forval i=1(1)5 {   
  estimates restore model1_i   
  local b11 = string(_b[`i'.tratamiento], "%9.3f")  
  local se11 = string(_se[`i'.tratamiento], "%9.3f")  
  local pvalue11 = 2*ttail(e(df_r),abs(`b11'/`se11'))   
  stars `pvalue11'  
  local stars11 "`stars'"  
  
  estimates restore model2_i  
  local b21 = string(_b[`i'.tratamiento], "%9.3f")   
  local se21 = string(_se[`i'.tratamiento], "%9.3f")   
  local pvalue21 = 2*ttail(e(df_r),abs(`b21'/`se21'))   
  stars `pvalue21'  
  local stars21 "`stars'"  
  
  if `i' == 0 {   
    local trat_label "\ \ Control"   
  }   
  else if `i' == 1 {   
    local trat_label "\ \ Placebo"   
  }   
  else if `i' == 2 {   
    local trat_label "\ \ Information"   
  }   
  else if `i' == 3 {   
    local trat_label "\ \ Social norms"   
  }   
  else if `i' == 4 {   
    local trat_label "\ \ Trust"   
  }   
  else if `i' == 5 {   
    local trat_label "\ \ Framing"   
  }   
    
  // Write the results to the table   
  texdoc write  `trat_label' & `b11'`stars11' & `b21'`stars21' \\  
  texdoc write  & (`se11') & (`se21') \\  
}  
  
// Estimates for 2.ingreso  
estimates restore model1_i   
local b122 = string(_b[2.ingreso], "%9.3f")  
local se122 = string(_se[2.ingreso], "%9.3f")  
local pvalue122 = 2*ttail(e(df_r),abs(`b122'/`se122')) 
stars `pvalue122'  
local stars122 "`stars'"  
  
estimates restore model2_i  
local b222 = string(_b[2.ingreso], "%9.3f")   
local se222 = string(_se[2.ingreso], "%9.3f")   
local pvalue222 = 2*ttail(e(df_r),abs(`b222'/`se222'))   
stars `pvalue222'  
local stars222 "`stars'"  


// Estimates for 2.ingreso  
estimates restore model1_i   
local b123 = string(_b[3.ingreso], "%9.3f")  
local se123 = string(_se[3.ingreso], "%9.3f")  
local pvalue123 = 2*ttail(e(df_r),abs(`b123'/`se123')) 
stars `pvalue123'  
local stars123 "`stars'"  
  
estimates restore model2_i  
local b223 = string(_b[2.ingreso], "%9.3f")   
local se223 = string(_se[2.ingreso], "%9.3f")   
local pvalue223 = 2*ttail(e(df_r),abs(`b223'/`se223'))   
stars `pvalue223'  
local stars223 "`stars'"  
  
texdoc write \textbf{Household income level} & & \\
texdoc write  {\ \ Medium} & `b122'`stars122' & `b222'`stars222' \\  
texdoc write  & (`se122') & (`se222') \\  
texdoc write  {\ \ High} & `b122'`stars123' & `b222'`stars223' \\  
texdoc write  & (`se123') & (`se223') \\    

texdoc write  \textbf{Treatment x Household income level} & & \\

// Write the estimates for i.tratamiento#2.ingreso  
forval i=1(1)5 {   
  estimates restore model1_i
  local b132 = string(_b[`i'.tratamiento#2.ingreso], "%9.3f")  
  local se132 = string(_se[`i'.tratamiento#2.ingreso], "%9.3f")  
  local pvalue132 = 2*ttail(e(df_r),abs(`b132'/`se132'))    
  stars `pvalue132'  
  local stars132 "`stars'"  
   
  estimates restore model2_i 
  local b232 = string(_b[`i'.tratamiento#2.ingreso], "%9.3f")   
  local se232 = string(_se[`i'.tratamiento#2.ingreso], "%9.3f")   
  local pvalue232 = 2*ttail(e(df_r),abs(`b232'/`se232'))   
  stars `pvalue232'  
  local stars232 "`stars'"  

  if `i' == 0 {   
    local trat_int_label "\ \ Control \& Middle income"   
  }   
  else if `i' == 1 {   
    local trat_int_label "\ \ Placebo \& Middle income"   
  }   
  else if `i' == 2 {   
    local trat_int_label "\ \ Information \& Middle income"   
  }   
  else if `i' == 3 {   
    local trat_int_label "\ \ Social norms \& Middle income"   
  }   
  else if `i' == 4 {   
    local trat_int_label "\ \ Trust \& Middle income"   
  }   
  else if `i' == 5 {   
    local trat_int_label "\ \ Framing \& Middle income"   
  }   
    
  // Write the results to the table   
  texdoc write  `trat_int_label' & `b132'`stars132' & `b232'`stars232' \\  
  texdoc write  & (`se132') & (`se232') \\  
}  
 
// Write the estimates for i.tratamiento#3.ingreso  
forval i=1(1)5 {   
  estimates restore model1_i
  local b133 = string(_b[`i'.tratamiento#3.ingreso], "%9.3f")  
  local se133 = string(_se[`i'.tratamiento#3.ingreso], "%9.3f")  
  local pvalue133 = 2*ttail(e(df_r),abs(`b133'/`se133'))    
  stars `pvalue133'  
  local stars133 "`stars'"  
  
  local cons1    = string( _b[_cons], "%9.3f")  
  local se_cons1 = string(_se[_cons], "%9.3f")  
  local pv_cons1 = 2*ttail(e(df_r),abs(`cons1'/`se_cons1'))  
  stars `pv_cons1'  
  local stars_cons1 "`stars'"
   
  estimates restore model2_i 
  local b233 = string(_b[`i'.tratamiento#3.ingreso], "%9.3f")   
  local se233 = string(_se[`i'.tratamiento#3.ingreso], "%9.3f")   
  local pvalue233 = 2*ttail(e(df_r),abs(`b233'/`se233'))   
  stars `pvalue233'  
  local stars233 "`stars'"   
 
  local cons2    = string( _b[_cons], "%9.3f")  
  local se_cons2 = string(_se[_cons], "%9.3f")  
  local pv_cons2 = 2*ttail(e(df_r),abs(`cons2'/`se_cons2'))   
  stars `pv_cons2'  
  local stars_cons2 "`stars'"
  
  if `i' == 0 {   
    local trat_int_label "\ \ Control \& High income"   
  }   
  else if `i' == 1 {   
    local trat_int_label "\ \ Placebo \& High income"   
  }   
  else if `i' == 2 {   
    local trat_int_label "\ \ Information \& High income"   
  }   
  else if `i' == 3 {   
    local trat_int_label "\ \ Social norms \& High income"   
  }   
  else if `i' == 4 {   
    local trat_int_label "\ \ Trust \& High income"   
  }   
  else if `i' == 5 {   
    local trat_int_label "\ \ Framing \& High income"   
  }   
    
  // Write the results to the table   
  texdoc write  `trat_int_label' & `b133'`stars133' & `b233'`stars233' \\  
  texdoc write  & (`se133') & (`se233') \\  
}  

texdoc write & & \\
 texdoc write Constant  & \multicolumn{1}{c}{`cons1' `stars_cons1'} & \multicolumn{1}{c}{`cons2' `stars_cons2'}  \\
texdoc write 	& \multicolumn{1}{c}{(`se_cons1')}  & \multicolumn{1}{c}{(`se_cons2')}  \\

texdoc write \addlinespace
texdoc write \hline
texdoc write Strata fixed effects & No & Yes \\
texdoc write Covariates & No & Yes  \\
texdoc write R-squared & `r21' & `r22' \\
texdoc write Observations & `n1'  & `n2'  \\

texdoc write \hline	
texdoc write \addlinespace
texdoc write \multicolumn{3}{p{\linewidth}}{\footnotesize Models 1 and 2 estimate the interaction between the treatment variable and the household income level that the girl belongs to. Model 2 includes as covariates whether a girl has received a dose before, healthcare center visited and age. Standard errors in parentheses. *** p$<$0.001, ** p$<$0.01, * p$<$0.5} \\

// Close the table  
texdoc write \end{tabular}  
texdoc write }  
texdoc write \end{table}  
texdoc close